/**
 * 2013 2013-5-8
 * @author sd;kghjsdkofgj
 */
package com.surfilter.dp.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;

import com.surfilter.dp.constant.Constants;
import com.surfilter.dp.context.ContextHolder;
import com.surfilter.dp.model.ModuleModel;

/**
 * @author sd;kghjsdkofgj
 * 
 */
@SuppressWarnings("serial")
public class ModuleDetailPanel extends JPanel {
	private JTextField textField_moduleName;
	private JTextField textField_moduleText;
	private JTextField textField_pname;
	private JTextField textField_moduleType;
	private JTextField textField_serviceName;
	private JTextField textField_remark;
	private JTextField textField_codePath;
	private JTextField textField_serviceCls;
	private JTextField textField_tableName;
	private JSpinner textField_sortIndex;
	private JButton btn_saveInfo;
	private JButton btn_delModule;
	private JButton btn_addModule;

	public ModuleDetailPanel() {
		initView();
		initListeners();

	}

	/**
	 * 
	 */
	private void initListeners() {
		btn_saveInfo.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				ContextHolder context = ContextHolder.getInstance();
				ModuleModel selected = context.getModuleTree().getSelectedModule(); 
				if(selected != null && !selected.getModuleName().equals("root")){
					ModuleModel module = getFormValue();
					context.getModuleDao().updateModule(module);
					context.getModuleTree().updateCurrentNode(module);
					JOptionPane.showMessageDialog( ContextHolder.getInstance().getMainWin(), "修改模块信息成功");
					context.getModuleTree().initData();
				}else{
					JOptionPane.showMessageDialog( ContextHolder.getInstance().getMainWin(), "没有选中模块或者选中了根模块");
				}
			}});
		btn_delModule.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				ContextHolder context = ContextHolder.getInstance();
				ModuleModel selected = context.getModuleTree().getSelectedModule();
				if(selected!=null && !selected.getModuleName().equals("root")){
					List<ModuleModel> childrenList = context.getModuleTree().getChildNodes(selected.getModuleName());
					if(childrenList.size() == 0){
						ModuleModel module = getFormValue();
						context.getModuleDao().deleteModule(module);
						context.getModuleTree().deleteCurrentNode();
						JOptionPane.showMessageDialog( ContextHolder.getInstance().getMainWin(), "删除模块成功");
						context.getModuleTree().initData();
					}else{
						JOptionPane.showMessageDialog( ContextHolder.getInstance().getMainWin(), "该模块包含子模块，不能删除");
					}
				}else {
					JOptionPane.showMessageDialog( ContextHolder.getInstance().getMainWin(), "没有选中模块或者选中了根模块");
				}
			}});
		btn_addModule.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				ContextHolder context = ContextHolder.getInstance();
				ModuleModel selected = context.getModuleTree().getSelectedModule();
				if(selected != null && selected.getModuleType() != Constants.TYPE_FUN){
					new ModuleAddDialog(selected,context.getMainWin()).setVisible(true);
				}else{
					JOptionPane.showMessageDialog( ContextHolder.getInstance().getMainWin(), "没有选中模块或者选中了功能模块");
				}
			}});
	}

	/**
	 * 
	 */
	private void initView() {
		setLayout(null);

		JLabel lblNewLabel = new JLabel("模块名称");
		lblNewLabel.setBounds(39, 37, 103, 15);
		this.add(lblNewLabel);

		textField_moduleName = new JTextField();
		textField_moduleName.setBounds(152, 34, 170, 21);
		this.add(textField_moduleName);
		textField_moduleName.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("展示名称");
		lblNewLabel_1.setBounds(346, 37, 97, 15);
		this.add(lblNewLabel_1);

		textField_moduleText = new JTextField();
		textField_moduleText.setBounds(453, 34, 176, 21);
		this.add(textField_moduleText);
		textField_moduleText.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("父模块");
		lblNewLabel_2.setBounds(56, 65, 86, 15);
		this.add(lblNewLabel_2);

		textField_pname = new JTextField();
		textField_pname.setBounds(152, 62, 170, 21);
		this.add(textField_pname);
		textField_pname.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("模块类型");
		lblNewLabel_3.setBounds(346, 65, 97, 15);
		this.add(lblNewLabel_3);

		textField_moduleType = new JTextField();
		textField_moduleType.setBounds(453, 62, 176, 21);
		this.add(textField_moduleType);
		textField_moduleType.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("Java服务类");
		lblNewLabel_4.setBounds(32, 93, 110, 15);
		this.add(lblNewLabel_4);

		textField_serviceName = new JTextField();
		textField_serviceName.setBounds(152, 90, 170, 21);
		textField_serviceName.setColumns(10);
		this.add(textField_serviceName);

		JLabel lblJs = new JLabel("JS服务类");
		lblJs.setBounds(346, 93, 97, 15);
		this.add(lblJs);

		textField_serviceCls = new JTextField();
		textField_serviceCls.setBounds(453, 90, 176, 21);
		textField_serviceCls.setColumns(10);
		this.add(textField_serviceCls);

		JLabel label_1 = new JLabel("模块备注");
		label_1.setBounds(44, 121, 91, 15);
		this.add(label_1);

		textField_remark = new JTextField();
		textField_remark.setBounds(152, 118, 170, 21);
		textField_remark.setColumns(10);
		this.add(textField_remark);

		JLabel label_2 = new JLabel("数据表名");
		label_2.setBounds(346, 121, 86, 15);
		this.add(label_2);

		textField_tableName = new JTextField();
		textField_tableName.setBounds(453, 118, 176, 21);
		textField_tableName.setColumns(10);
		this.add(textField_tableName);

		JLabel label = new JLabel("包路径");
		label.setBounds(56, 149, 60, 15);
		this.add(label);

		textField_codePath = new JTextField();
		textField_codePath.setBounds(152, 146, 170, 21);
		textField_codePath.setColumns(10);
		this.add(textField_codePath);

		JLabel label_3 = new JLabel("模块排序");
		label_3.setBounds(346, 149, 97, 15);
		this.add(label_3);

		textField_sortIndex = new JSpinner();
		textField_sortIndex.setBounds(453, 146, 176, 22);
		this.add(textField_sortIndex);

		btn_saveInfo = new JButton("保存信息");
		btn_saveInfo.setBounds(75, 253, 122, 23);
		this.add(btn_saveInfo);

		btn_delModule = new JButton("删除该模块");
		btn_delModule.setBounds(241, 253, 122, 23);
		this.add(btn_delModule);

		btn_addModule = new JButton("在该模块添加子模块");
		btn_addModule.setBounds(402, 253, 170, 23);
		this.add(btn_addModule);
		
		JLabel lblNewLabel_5 = new JLabel("注1：模块名称为模块的唯一标识，必须保证唯一性");
		lblNewLabel_5.setBounds(75, 303, 506, 28);
		add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("注2：包路径必须为JS服务类子集，如JS服务类为Service.aaa.bbb.Test,则包路径为aaa.bbb");
		lblNewLabel_6.setBounds(75, 341, 554, 15);
		add(lblNewLabel_6);
		
		JLabel label_4 = new JLabel("注3：一个模块可以对应多个数据表，表名以逗号隔开");
		label_4.setBounds(75, 375, 554, 15);
		add(label_4);
		
	}

	public void setFormValue(ModuleModel model) {
		textField_moduleName.setText(model.getModuleName());
		textField_moduleName.setEditable(false);
		textField_moduleText.setText(model.getModuleText());
		textField_pname.setText(model.getPname());
		textField_pname.setEditable(false);
		textField_moduleType.setText(model.getModuleType()==Constants.TYPE_FOLDER?"目录模块":"功能模块");
		textField_moduleType.setEditable(false);
		textField_serviceName.setText(model.getServiceName());
		textField_remark.setText(model.getRemark());
		textField_codePath.setText(model.getCodePath());
		textField_serviceCls.setText(model.getServiceCls());
		textField_tableName.setText(model.getTableName());
		textField_sortIndex.setValue(model.getSortIndex());
	}
	
	
	public ModuleModel getFormValue(){
		ModuleModel model = new ModuleModel();
		model.setModuleName(textField_moduleName.getText());
		model.setModuleText(textField_moduleText.getText());
		model.setPname(textField_pname.getText());
		model.setModuleType(textField_moduleType.getText().equals("目录模块")?Constants.TYPE_FOLDER:Constants.TYPE_FUN);
		model.setServiceName(textField_serviceName.getText());
		model.setRemark(textField_remark.getText());
		model.setCodePath(textField_codePath.getText());
		model.setServiceCls(textField_serviceCls.getText());
		model.setTableName(textField_tableName.getText());
		model.setSortIndex((Integer)textField_sortIndex.getValue());
		return model;
	}
}
